<?php
/**
 * Logo_Controller
 * - Controller for module page Logo
 * @author ANHTAM
 * @since 1.1 - Sep 27, 2011
 * $Id: $
 */
// Require module for controller
require_once(LIB_DAO_DIR . "/MFlash.class.php");

class Flash_Controller extends Controller {
	/**
     * - Constructor
     */
    public function __construct() {
    	parent::__construct();
    	$this->flagMenuImage = true;
    	$this->langue = true;
    	$this->getSession = Session::Get("USER");
    	if ($this->getSession['langue'] == 'en') {
    		$this->langue = false;
    	}
    }
    
    /**
     * actionIndex
     * - page index of controller
     */
    public function actionIndex() {
    	// Process init
        $objView = new View ();
        $this->tpl_title = 'Danh flash';
        $this->tpl_title_list = 'Quản lý hình ảnh -> Quản lý flash : 1=>Logo, 2=>Header, 3=>Banner';
        
    	if (count ($this->getSession) <= 0) {
        	$this->sendRedirect();
        }
        
        $ojbFlash = new MFlash();
    	
    	// List all
    	$this->arrFlash = $ojbFlash->getAllByAttribute(array());

        // Process view
        $objView->assignobj($this);
        $objView->display(false);
    }
    
	/**
     * actionEdit
     * - page edit of controller
     */
    public function actionEdit() {
    	
    	// Process init
        $objView = new View ();
        $this->tpl_title = 'Sửa tên danh mục';
        $this->tpl_title_list = 'Quản lý hình ảnh -> Sửa flash';
        $this->tpl_button = "Thêm mới";
        $this->arrField = array();
        
    	if (count ($this->getSession) <= 0) {
        	$this->sendRedirect();
        }
        
    	$ojbFlash = new MFlash();
    	
    	if ($this->isPost()) {
        	$arrPost = array('nameimage', 'item', 'mode', 'type');
        	$arrPostValue = FunctionCommon::getPost($arrPost);
        	
        	$this->id = $arrPostValue['item'];
        	
        	if ($arrPostValue['mode'] != '') {
        		
        		// Update du lieu
        		if ($arrPostValue['type'] == 'edit' && $this->id != '') {
        			$ojbFlash->setAttribute('code', $arrPostValue['nameimage']);
		        	$ojbFlash->update(array('id' => $this->id));
		        	$this->sendRedirect("index.php?m=flash");
        		}
        		
        		// Xu ly file
	        	if ($arrPostValue['type'] == 'upload') {
		        	if (isset ($_FILES["code"]["name"]) && $_FILES["code"]["name"] != '') {
			        	$this->tenFile = date("dHis")."_".$_FILES["code"]["name"];
			        	if (Validator::validateTypeImage($this->tenFile)) {
			        		if (FunctionCommon::uploadFileTo($_FILES["code"]["tmp_name"], HTDOC_IMAGES_DIR."/flash", $this->tenFile)) {
			        			// Delete image
			        			FunctionCommon::move(HTDOC_IMAGES_DIR."/flash/".$arrPostValue['nameimage']);
			        		}
			        	}
		        	} else {
		        		$this->tenFile = $arrPostValue['nameimage'];
		        	}
		        }
		        
		        // Delete image
	        	if ($arrPostValue['type'] == 'delete') {
		        	$tenFile = $arrPostValue['nameimage'];
		        	FunctionCommon::move(HTDOC_IMAGES_DIR."/flash/".$tenFile);
		        }
		        
		        $this->arrField = $arrPostValue;
        	} else {
        		$this->tpl_title = 'Sửa Flash';
        		$this->tpl_button = "Sửa";
	        	$this->arrField = $ojbFlash->getByPrimaryKey($this->id);
	        	$this->tenFile = $this->arrField["code"];
        	}
    	}

        // Process view
        $objView->assignobj($this);
        $objView->display(false);
    }
}
?>

